﻿using System;
using System.Threading.Tasks;
using System.Collections.Concurrent;
using System.Text;
using NoticeHelper.Dingtalk.DingtalkCustomRobotSdk;

namespace Helpers
{
    public class NoticeHelper
    {
        public static ConcurrentDictionary<string, DateTimeOffset> _keyValuePairs = new ConcurrentDictionary<string, DateTimeOffset>();
        public static async Task<string> NoticeAsync(string title, Exception ex) => await NoticeAsync(new NoticeInput
        {
            Title = title,
            Body = $"{ex.Message}{Environment.NewLine}{ex}"
        }).ConfigureAwait(false);

        public static async Task<string> NoticeAsync(NoticeInput input)
        {
            StringBuilder retMsg = new StringBuilder();
            try
            {
                if (input == null || NoticeOptions.Instance == null)
                {
                    return null;
                }

                //处理发送间隔
                long IntervalSeconds = 0;
                if (input.IntervalSeconds > 0) IntervalSeconds = input.IntervalSeconds;
                if (IntervalSeconds <= 0) NoticeOptions.Instance.IntervalMap?.TryGetValue(input.Code, out IntervalSeconds);
                if (IntervalSeconds <= 0 && NoticeOptions.Instance.IntervalSeconds > 0) IntervalSeconds = NoticeOptions.Instance.IntervalSeconds;
                if (IntervalSeconds > 0)
                {
                    if (_keyValuePairs.TryGetValue(input.Code, out var time) && (DateTimeOffset.Now - time).TotalSeconds < IntervalSeconds)
                    {
                        return null;
                    }
                    else
                    {
                        _keyValuePairs.AddOrUpdate(input.Code, x => DateTimeOffset.Now, (_, __) => DateTimeOffset.Now);
                    }
                }

                if (NoticeOptions.Instance.UseEmail)
                {
                    try
                    {
                        var ret = await MailHelper.SendAsync(new SendMailInput
                        {
                            Subject = input.Title,
                            Body = input.Body
                        }).ConfigureAwait(false);
                        if (!ret)
                        {
                            retMsg.AppendLine($"发送邮件失败");
                        }
                    }
                    catch (Exception ex)
                    {
                        retMsg.AppendLine($"发送邮件异常,{ex.Message}");
                    }
                }

                if (NoticeOptions.Instance.UseDingTalk)
                {
                    try
                    {
                        var ret = await DingtalkClient.Instance
                                       .SendMarkdownAsync(input.Title, $"# {input.Title}{Environment.NewLine}{input.Body}")
                                       .ConfigureAwait(false);
                        if (ret.ErrCode != 0)
                        {
                            retMsg.AppendLine($"发送钉钉失败,{ret.ErrCode}--{ret.ErrMsg}");
                        }
                    }
                    catch (Exception ex)
                    {
                        retMsg.AppendLine($"发送钉钉异常,{ex.Message}");
                    }
                }
            }
            catch (Exception ex)
            {
                retMsg.AppendLine($"发送异常,{ex.Message}");
            }
            return retMsg.ToString();
        }
    }
}
